import CryptoJS from "crypto-js"
import { isString } from "./is"

const KEY = "zhou-admin"

/**
 * * 加密
 * @param data { string }
 * @returns
 */
export const cryptoEncode = (data: string): string => {
  if (!isString(data)) return ""
  // 加密
  try {
    const encryptedData = CryptoJS.AES.encrypt(data, KEY, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    }).toString()
    return encryptedData
  } catch (error: any) {
    console.log("cryptoEncode", error.message)
    return data
  }
}

/**
 * * 解密
 * @param data { string }
 * @returns
 */
export const cryptoDecode = (data: string): string => {
  if (!isString(data)) return ""
  // 解密
  try {
    const decryptedData = CryptoJS.AES.decrypt(data, KEY, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    })
    return decryptedData.toString(CryptoJS.enc.Utf8)
  } catch (error: any) {
    console.log("cryptoDecode", error.message)
    return ""
  }
}
